﻿﻿function findElement(el,element){ // 寻找tagName=element的el父对象，'element'是标签名
  newel = el;
  while(newel.tagName.toLowerCase()!=element && newel.parentNode){
    newel = newel.parentNode;
  }
  if(newel.tagName.toLowerCase() == element) return newel;
  else return false;
}


var gdCtrl = new Object();
var gcGray   = "#cccccc";
var gcToggle = "#f2f6f9"; //#e5eaef
var gcBG = "#ffffff";
var previousObject = null;

var gdCurDate = new Date();
var cYear = gdCurDate.getFullYear();
var cMonth = gdCurDate.getMonth()+1;
var cDay = gdCurDate.getDate();
var giYear = gdCurDate.getFullYear();
var giMonth = gdCurDate.getMonth()+1;
var giDay = gdCurDate.getDate();

var closeFlag = 0;
var blnFlag = true;


// function isValidDate() added by weng.jianhuan at 2004-04-30   判断两个日期的大小

// 输入框命名为：name=start,name=end
// 当页面上有多对发布起始、到期日输入框时，输入框命名为：name=start_1,name=end_1,name=start_2,name=end_2......字母和数字必须用下划线'_'分开
var errorText = '错误的日期！';
function isValidDate(whichDate,y,m,d){
  nameStr = whichDate.name;
  nameArr = nameStr.split('_');
  if(nameArr[0] == 'start'){
     errorText = '起始日不能大于结束日！';
	 if(parForm = findElement(whichDate,'form')){ 
         if(nameArr.length > 1) objEnd = parForm.elements['end_'+nameArr[1]];
	     else objEnd = parForm.elements['end'];
	 }
	 else{
	        if(nameArr.length > 1) objEnd = eval("document.getElementById('end_" + nameArr[1] + "')");
	        else objEnd = eval("document.getElementById('end')");
	 }
	 if(objEnd.value == '') return true;
	 else { var dateEnd = objEnd.value;
	         dateArr = dateEnd.split('-');
			if((y*10000+m*100+d*1)>(dateArr[0]*10000+dateArr[1]*100+dateArr[2]*1)) return false;
	 }
   }
			//var tDate = new Date();
			//if(tDate.setFullYear(y,m,d)>tDate.setFullYear(dateArr[0],dateArr[1],dateArr[2])) return false;}}
   else if(nameArr[0] == 'end'){
     errorText = '结束日不能小于起始日！';
     if(parForm = findElement(whichDate,'form')){ 
         if(nameArr.length > 1) objStart = parForm.elements['start_'+nameArr[1]];
	     else objStart = parForm.elements['start'];
	 }
	 else{
	        if(nameArr.length > 1) objStart = eval("document.getElementById('start_" + nameArr[1] + "')");
	        else objStart = eval("document.getElementById('start')");
	 }
	 if(objStart.value == '') return true;
	 else { var dateStart = objStart.value;
	         dateArr = dateStart.split('-');
			if((y*10000+m*100+d*1)<(dateArr[0]*10000+dateArr[1]*100+dateArr[2]*1)) return false;
	 }
    }
			//var tDate = new Date();
			//if(tDate.setFullYear(y,m,d)<tDate.setFullYear(dateArr[0],dateArr[1],dateArr[2])) return false;}}
  return true;
}

/********************************************* pass strDate to 'dateCtrl' *********************************************/
var functionAfterSetDate = null;
function fSetDate(iYear,iMonth,iDay){
  VicPopCal.style.visibility = "hidden";
  if ((iYear == 0) && (iMonth == 0) && (iDay == 0)){
  	gdCtrl.value = "";
  }else{
  	iMonth = iMonth + 100 + "";
  	iMonth = iMonth.substring(1);
  	iDay   = iDay + 100 + "";
  	iDay   = iDay.substring(1);
  	if(gdCtrl.tagName == "INPUT"){
	   // 发布其始日 和 发布终止日 （调用isValidDate()判断两个日期的大小） added by weng.jianhuan
	   if(gdCtrl.name.indexOf('start') > -1 || gdCtrl.name.indexOf('end') > -1){
  	    if(isValidDate(gdCtrl,iYear, iMonth, iDay)) gdCtrl.value = iYear+"-"+iMonth+"-"+iDay;
		else { HiddenDiv();alert(errorText);return false;}}
	   //单个日期输入 (输入框name值里不能含有"start"和"end")
	   else gdCtrl.value = iYear+"-"+iMonth+"-"+iDay;
  	}else{
  	  // 发布其始日 和 发布终止日 （调用isValidDate()判断两个日期的大小）  added by weng.jianhuan
	  if(gdCtrl.name.indexOf('start') > -1 || gdCtrl.name.indexOf('end') > -1){
	    if(isValidDate(gdCtrl,iYear, iMonth, iDay))	gdCtrl.innerText = iYear+"-"+iMonth+"-"+iDay;
	    else { HiddenDiv();alert(errorText);return false;}}
	  //单个日期输入 (输入框name值里不能含有"start"和"end")
	  else gdCtrl.value = iYear+"-"+iMonth+"-"+iDay;
  	}
  }
  
  if(functionAfterSetDate!=null && typeof functionAfterSetDate =='function') functionAfterSetDate();//edited by weng 2005.10.26
  
  if(VicPopCal.arrToggleTags){
     for(var i=0;VicPopCal.arrToggleTags[i];i++) VicPopCal.arrToggleTags[i].style.visibility = "visible";
     VicPopCal.arrToggleTags.length=0;
  }
  fUpdateCal(iYear,iMonth,iDay);
  window.returnValue=gdCtrl.value;
  //window.close();
}
function fSetSelected(aCell){
  var iOffset = 0;
  try{
  var iYear = parseInt(tbSelYear.value);
  var iMonth = parseInt(tbSelMonth.value);
  }catch(e){}
  
  aCell.bgColor = gcBG;
  aCell.bgColor = '#CFDBF1';
  with (aCell.childNodes[0]){
  	var iDay = parseInt(innerHTML);
  	//if (color==gcGray) iOffset = (Victor<10)?-1:1;
  	if(getAttribute('flagMonth')==0) iOffset = (getAttribute('Victor')<10)?-1:1;
	iMonth += iOffset;
	if (iMonth<1) {
		iYear--;
		iMonth = 12;
	}else if (iMonth>12){
		iYear++;
		iMonth = 1;
	}
  }
  fSetDate(iYear, iMonth, iDay);
}
/********************************************* pass strDate to 'dateCtrl' *********************************************/

function closeCalendar(setType){
  if(setType == "on"){
    if(closeFlag != 0){
      clearTimeout(closeFlag);
      closeFlag = 0;
    }
  }else{
    if(VicPopCal.style.visibility == "visible"){
      closeFlag = setTimeout("HiddenDiv2()",1000);
      blnFlag = true;
    }
  }
}

function HiddenDiv(){	
	var i;
    VicPopCal.style.visibility = "hidden";
    if(VicPopCal.arrToggleTags){
       for (i=0;VicPopCal.arrToggleTags[i];i++) VicPopCal.arrToggleTags[i].style.visibility = "visible";
	   VicPopCal.arrToggleTags.length=0;
	}
    closeCalendar('on');
}

function HiddenDiv2(){	
	if(blnFlag){
  	   var i;
       VicPopCal.style.visibility = "hidden";
       if(VicPopCal.arrToggleTags){
          for (i=0;VicPopCal.arrToggleTags[i];i++) VicPopCal.arrToggleTags[i].style.visibility = "visible";
	      VicPopCal.arrToggleTags.length=0;
	   }
       closeCalendar('on');
  }
}

function fBuildCal(iYear, iMonth, iDay) {
  var aMonth=new Array();
  for(i=1;i<7;i++) aMonth[i]=new Array(10);
  
  var dCalDate=new Date(iYear, iMonth-1, 1);
  var iDayOfFirst=dCalDate.getDay();
  var iDaysInMonth=new Date(iYear, iMonth, 0).getDate();
  var iOffsetLast=new Date(iYear, iMonth-1, 0).getDate()-iDayOfFirst+1;
  var iDate = 1;
  var iNext = 1;

  for (d = 0; d < 7; d++){
	  aMonth[1][8]=iYear;
	  aMonth[1][9]=iMonth;
	  aMonth[1][d] = (d<iDayOfFirst)?-(iOffsetLast+d):iDate++;}
  for (w = 2; w < 7; w++)
  	for (d = 0; d < 7; d++){
		aMonth[w][8]=iYear;
		aMonth[w][9]=iMonth;
		aMonth[w][d] = (iDate<=iDaysInMonth)?iDate++:-(iNext++);
	}
  return aMonth;
}

function tdmouseover(){
gcBG=this.bgColor;this.bgColor=gcToggle;
}
function tdmouseout(){
this.bgColor=gcBG;
}
var strError = ['该日期不能添加！','截止时间必须大于当前日期！','截止时间不能超过合同结束时间！'];
var numError = 0;
function tdonclickerror(numError){
alert(strError[numError]);return false;
}

/**
*画出日期选择框
*@param iYear 
**/
function fDrawCal(iYear, iMonth, iCellHeight, sDateTextSize) {
  var WeekDay = new Array("日","一","二","三","四","五","六");
  var styleTD = "valign='middle' align='center' height='"+iCellHeight+"' style='font:bold arial "+sDateTextSize+";";   //padding-top:4px;padding-bottom:3px;  
  var kkjj = "";
	
	kkjj += "<tr>";
	for(i=0; i<7; i++){
		kkjj += "<td align='center'>"+ WeekDay[i] + "</td>";
	}
	kkjj += "</tr>";
  	for (w = 1; w < 7; w++) {
		kkjj += "<tr>";
		for (d = 0; d < 7; d++) {
			kkjj += "<td id=calCell bgcolor='#ffffff' "+styleTD+"cursor:pointer;' onMouseOver='gcBG=this.bgColor;this.bgColor=gcToggle' onMouseOut='this.bgColor=gcBG' onclick='fSetSelected(this)'>";
			kkjj += "<a id=cellText flagMonth='' Victor='Liming Weng' onclick='return false;'> </a>";
			kkjj += "</td>";
		}
		kkjj += "</tr>";
    }
	return kkjj;
}

function fUpdateCal(iYear, iMonth, iDate) {
  myMonth = fBuildCal(iYear, iMonth, iDate);
  var num = 0;
  var tempY,tempM,tempD;
  var flag = true;
    
    //added by weng,for NS
    var objFont = document.getElementsByTagName('a');
    var cellText = new Array();
    for(var i=0;i<objFont.length;i++) if(objFont[i].getAttribute('id')=='cellText') cellText[cellText.length] = objFont[i];
    
    for (w = 0; w < 6; w++)
  	for (d = 0; d < 7; d++)
  		with (cellText[(7*w)+d]) {
			
			if(myMonth[w+1][d]<0){
			    if(w<2){tempM = (iMonth*1-1<1)?12:iMonth*1-1;if(tempM==12) tempY = iYear*1-1;else tempY = iYear*1;}
				if(w>3){tempM = (iMonth*1+1>12)?1:iMonth*1+1;if(tempM==1) tempY = iYear*1+1;else tempY = iYear*1;}
				tempD = -myMonth[w+1][d];
			}
			else {tempY = iYear;tempM = iMonth;tempD = myMonth[w+1][d]}
			intDate = tempY*10000+tempM*100+tempD*1;
			
			if((beginning>0 && intDate<=beginning) || (deadline>0 && intDate>deadline)){
				if(intDate<=beginning) numError = 1;
				else if(intDate>deadline) numError = 2;
				
				parentNode.onmouseover = null;
			    parentNode.onmouseout = null;
				parentNode.onclick = new Function('tdonclickerror('+numError+')');
				parentNode.style.cursor = 'default';
				style.cursor = 'default';
				parentNode.bgColor='#F2F6F9';
				flag = false;
			}
			else {
			    parentNode.onmouseover = tdmouseover;
				parentNode.onmouseout = tdmouseout;
				parentNode.onclick = new Function('fSetSelected(this);return false;');
				parentNode.style.cursor = 'pointer';
				style.cursor = 'pointer';
				parentNode.bgColor='#ffffff';
				flag = true;
			}
			
			//alert(myMonth[w+1][d]);
  			setAttribute('Victor',num++);
  			if (myMonth[w+1][d]<0) {
  				setAttribute('flagMonth',0);
  				style.color = gcGray;
  				innerHTML = -myMonth[w+1][d];
  				
  				if(flag) parentNode.bgColor='#ffffff';//added by weng,2005.9.29]
				//alert(myMonth[w+1][8]+"-->"+myMonth[w+1][9]);
				if(myMonth[w+1][8]==iYear && myMonth[w+1][9]==iMonth && myMonth[w+1][d]==iDate)parentNode.bgColor='#999999';
  			}else{
  				setAttribute('flagMonth',1);
  				style.color = ((d==0)||(d==6))?"#ff6600":"#333333";
  				innerHTML = myMonth[w+1][d];
  				
  				if(iYear==cYear && iMonth==cMonth && myMonth[w+1][d]==cDay) parentNode.bgColor='#CFDBF1'; //标示today的bgColor
  				else if(flag) parentNode.bgColor='#ffffff';//added by weng,2005.9.29
				if(myMonth[w+1][8]==iYear && myMonth[w+1][9]==iMonth && myMonth[w+1][d]==iDate)parentNode.bgColor='#999999';
  			}

  		}
		if(typeof minY=="undefined" && typeof maxY=="undefined"){
		   if(iYear<=2000&&iMonth<=1) document.getElementById('PrevMonth').disabled = true;
  		   else document.getElementById('PrevMonth').disabled = false;
		   if(iYear>=2011&&iMonth>=12) document.getElementById('NextMonth').disabled = true;
 		   else document.getElementById('NextMonth').disabled = false;
		}else{
		   if(iYear<=minY&&iMonth<=1) document.getElementById('PrevMonth').disabled = true;
  		   else document.getElementById('PrevMonth').disabled = false;
		   if(iYear>=maxY&&iMonth>=12) document.getElementById('NextMonth').disabled = true;
 		   else document.getElementById('NextMonth').disabled = false;	
		}
}

function fSetYearMon(iYear, iMon, iDate){
  /*tbSelMonth.options[iMon-1].selected = true;
  for (i = 0; i < tbSelYear.length; i++)
	if (tbSelYear.options[i].value == iYear)
		tbSelYear.options[i].selected = true;*/
  tbSelYear.value=iYear;
  tbSelMonth.value=iMon;
  fUpdateCal(iYear, iMon, iDate);
}

function fPrevMonth(){
  var iMon = tbSelMonth.value;
  var iYear = tbSelYear.value;
  
  if (--iMon<1) {
	  iMon = 12;
	  iYear--;
  }
  
  fSetYearMon(iYear, iMon, null);
  
  if(typeof minY=="undefined" && typeof maxY=="undefined"){
     if(iYear<=2000&&iMon<=1) document.getElementById('PrevMonth').disabled = true;
     else document.getElementById('PrevMonth').disabled = false;
  }else{
	 if(iYear<=minY&&iMon<=1) document.getElementById('NextMonth').disabled = true;
     else document.getElementById('NextMonth').disabled = false;
  }
}

function fNextMonth(){
  var iMon = tbSelMonth.value;
  var iYear = tbSelYear.value;
  
  if (++iMon>12) {
	  iMon = 1;
	  iYear++;
  }
  
  fSetYearMon(iYear, iMon, null);
  
  if(typeof minY=="undefined" && typeof maxY=="undefined"){
    if(iYear>=2011&&iMon>=12) document.getElementById('NextMonth').disabled = true;
    else document.getElementById('NextMonth').disabled = false;
  }else{
	if(iYear>=maxY&&iMon>=12) document.getElementById('NextMonth').disabled = true;
    else document.getElementById('NextMonth').disabled = false;
  }
}

// Main: popCtrl is the widget beyond which you want this calendar to appear;
//       dateCtrl is the widget into which you want to put the selected date.
//       if(flagReventCal=0) display“清空”link
//       fnName:fSetDate()后执行的function
//       bDate:可以添加(fSetDate())的时间范围的开始时间    dDate:可以添加(fSetDate())的时间范围的结束时间
//       strDate:默认日期
// i.e.: <input type="text" name="dc" style="text-align:center" readonly><INPUT type="button" value="V" onclick="fPopCalendar(dc,dc);return false">
var beginning = 0;
var deadline = 0;
function fPopCalendar(popCtrl, dateCtrl, flagReventCal, fnName, bDate, dDate, strDate){
	var mY,maY;
	if(bDate)
	mY = bDate.substring(0,4);
	
	if(dDate)
	maY= dDate.substring(0,4);
	if(mY&&maY)
	initDateDiv(mY,maY);
	else if(typeof minY!="undefined" && typeof maxY!="undefined")
	initDateDiv(minY,maxY);
	else
	initDateDiv(2000,2011);
	
  if(flagReventCal && document.getElementById('aReventCal')) document.getElementById('aReventCal').style.display='none';
  if(fnName) functionAfterSetDate=fnName;
  else functionAfterSetDate=null;
  if(bDate) {var arrBDate = bDate.toString().split('-');beginning = arrBDate[0]*10000+arrBDate[1]*100+arrBDate[2]*1;}
  else beginning = 0;
  if(dDate) {var arrDDate = dDate.toString().split('-');deadline = arrDDate[0]*10000+arrDDate[1]*100+arrDDate[2]*1;}
  else deadline = 0;
  
  if (popCtrl == previousObject){
	  	if (VicPopCal.style.visibility == "visible"){
  		HiddenDiv();
  		return true;    
  	}
  }
  
  previousObject = popCtrl;
  gdCtrl = dateCtrl;
  
  if(dateCtrl.value&&dateCtrl.value!=""){
  fInitialDate(dateCtrl.value);
  }else if(strDate)fInitialDate(strDate);
  else
  fSetYearMon(giYear, giMonth, null); 
  
  var aToday = document.getElementById('aToday');
  if(aToday && (beginning>0 || deadline>0)){
     var dateToday = giYear*10000+giMonth*100+giDay*1;
	 if(dateToday<=beginning || dateToday>deadline) aToday.onclick=new Function('tdonclickerror(1);return false;');
  }
  else if(aToday) aToday.onclick=new Function('fSetDate('+giYear+','+giMonth+','+giDay+');return false;');
  
  var point = fGetXY(popCtrl);
  VicPopCal.arrToggleTags = new Array();
  VicPopCal.exCon = 'getAttribute(\'Victor\')!=\'Won\'';
  displayLayer('VicPopCal',point.x,point.y+16,'Popup2');
  
  blnFlag = false;
}


function fInitialDate(strDate){
	if( strDate == null || strDate.length != 10 ){
		giYear  = gdCurDate.getFullYear();
		giMonth = gdCurDate.getMonth()+1;
		giDay   = gdCurDate.getDate();	

		return false;
	}

	var sYear  = strDate.substring(0,4);
	var sMonth = strDate.substring(5,7);
	var sDay   = strDate.substring(8,10);

	if( sMonth.charAt(0) == '0' ) { sMonth = sMonth.substring(1,2); }
	if( sDay.charAt(0)   == '0' ) { sDay   = sDay.substring(1,2);   }

	var nYear  = parseInt(sYear );
	var nMonth = parseInt(sMonth);
	var nDay   = parseInt(sDay  );
	
	if ( isNaN(nYear ) )	return false;
	if ( isNaN(nMonth) )	return false;
	if ( isNaN(nDay  ) )	return false;

	var arrMon = new Array(12);
	arrMon[ 0] = 31;	arrMon[ 1] = nYear % 4 == 0 ? 29:28;
	arrMon[ 2] = 31;	arrMon[ 3] = 30;
	arrMon[ 4] = 31;	arrMon[ 5] = 30;
	arrMon[ 6] = 31;	arrMon[ 7] = 31;
	arrMon[ 8] = 30;	arrMon[ 9] = 31;
	arrMon[10] = 30;	arrMon[11] = 31;

	if ( nYear  < 1900 || nYear > 2100 )			return false;
	if ( nMonth < 1 || nMonth > 12 )				return false;
	if ( nDay < 1 || nDay > arrMon[nMonth - 1] )	return false;
    fSetYearMon(nYear, nMonth, nDay); 
	//giYear  = nYear;
	//giMonth = nMonth;
	//giDay   = nDay;
	return true;
}
var VicPopCal = new Object();
var tbSelMonth = new Object();
var tbSelYear = new Object();

function initDateDiv(mY,maY){
var gMonths = new Array("一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月");
	var divHtml = "";
	divHtml += "<table id='TableCal' border='0' bgcolor='#ffffff' style='border:#596e8b 1px solid;'>";
	divHtml += "<TR>";
	divHtml += "<td valign='middle' align='center'><input type='button' class='button' title='上一个月' name='PrevMonth' id='PrevMonth' value='<' style='height:18;width:20;FONT:bold' onClick='fPrevMonth()'>";
	divHtml += "&nbsp;<SELECT name='tbSelYear' id='tbSelYear' style='width:80px;' onChange='fUpdateCal(tbSelYear.value, tbSelMonth.value, null)' Victor='Won'>";
	
	if(typeof mY=="undefined" && typeof maY=="undefined"){
		for(i=2000;i<2012;i++)
			divHtml += "<OPTION value='"+i+"'>"+i+" 年</OPTION>";
	}else{ 
		if(maY<mY){
			for(i=2000;i<2012;i++)
				divHtml += "<OPTION value='"+i+"'>"+i+" 年</OPTION>";
		}else
			for(i=mY;i<=maY;i++)
				divHtml += "<OPTION value='"+i+"'>"+i+" 年</OPTION>";
		}
		divHtml += "</SELECT>";
		divHtml += "&nbsp;<select name='tbSelMonth' id='tbSelMonth' style='width:80px;' onChange='fUpdateCal(tbSelYear.value, tbSelMonth.value, null)' Victor='Won'>";
		for (i=0; i<12; i++)
			divHtml += "<option value='"+(i+1)+"'>"+gMonths[i]+"</option>";
		divHtml += "</SELECT>";
		divHtml += "&nbsp;<input type='button' class='button' title='下一个月' name='NextMonth' id='NextMonth' value='>' style='height:18;width:20;FONT:bold' onclick='fNextMonth()'>";
		divHtml += "</td>";
		divHtml += "</TR><TR>";
		divHtml += "<td align='center' bgcolor='#CFDBF1' class='tb_border' style='padding:0px;'>";
		divHtml += "<DIV style='padding:0px;'><table width='100%' border='0' cellspacing='1' cellpadding='0'>";
		divHtml += fDrawCal(giYear, giMonth, 20, '12');
		divHtml += "</table></DIV>";
		divHtml += "</td>";
		divHtml += "</TR><TR><TD align='center'>";
		divHtml += "<TABLE width='100%'><TR><TD>";
		divHtml += "<a id='aReventCal' href='#' style='text-decoration:none' onclick='fSetDate(0,0,0);return false;' onMouseOver='this.style.color=\"#000066\"' onMouseOut='this.style.color=\"#333333\"'><b>清空</b></a>&nbsp;&nbsp;";
		divHtml += "<a href='#' id='aToday' style='text-decoration:none' onclick='fSetDate(giYear,giMonth,giDay);return false;' onMouseOver='this.style.color=\"#000066\"' onMouseOut='this.style.color=\"#333333\"'><b>今天: "+giYear+"-"+giMonth+"-"+giDay+"</b></a>";
		divHtml += "</td><td algin='right'>";
		divHtml += "<a href='#' style='text-decoration:none' onclick='HiddenDiv();return false;' onMouseOver='this.style.color=\"#000066\"' onMouseOut='this.style.color=\"#333333\"'><b>关闭</b></a>";
		divHtml += "</td></tr></table>";
		divHtml += "</TD></TR>";
		divHtml += "</TABLE></Div>";
		
		//document.getElementById('VicPopCal')
		VicPopCal = document.getElementById('VicPopCal');
		VicPopCal.innerHTML=divHtml;
		tbSelMonth = document.getElementById('tbSelMonth');
		tbSelYear = document.getElementById('tbSelYear');
}

document.write("<Div id='VicPopCal' name='VicPopCal' style='OVERFLOW:hidden;POSITION:absolute;VISIBILITY:hidden;border:0px ridge;z-index:100;' onmouseover='blnFlag=false;' onclick='blnFlag=false;'></Div>");

//initDateDiv();
document.onready=initDateDiv;

document.onclick = function(){
	VicPopCal = document.getElementById('VicPopCal');
	HiddenDiv2();
	if(!blnFlag) blnFlag=!blnFlag;
}
document.onmouseover=function(){
	blnFlag=true;
}